package brand

import (
	
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/model/brand"
    brandReq "github.com/flipped-aurora/gin-vue-admin/server/model/brand/request"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type BrandApi struct {}



// CreateBrand 创建品牌管理
// @Tags Brand
// @Summary 创建品牌管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body brand.Brand true "创建品牌管理"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /brands/createBrand [post]
func (brandsApi *BrandApi) CreateBrand(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	var brands brand.Brand
	err := c.ShouldBindJSON(&brands)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = brandsService.CreateBrand(ctx,&brands)
	if err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:" + err.Error(), c)
		return
	}
    response.OkWithMessage("创建成功", c)
}

// DeleteBrand 删除品牌管理
// @Tags Brand
// @Summary 删除品牌管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body brand.Brand true "删除品牌管理"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /brands/deleteBrand [delete]
func (brandsApi *BrandApi) DeleteBrand(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	brandId := c.Query("brandId")
	err := brandsService.DeleteBrand(ctx,brandId)
	if err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteBrandByIds 批量删除品牌管理
// @Tags Brand
// @Summary 批量删除品牌管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /brands/deleteBrandByIds [delete]
func (brandsApi *BrandApi) DeleteBrandByIds(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	brandIds := c.QueryArray("brandIds[]")
	err := brandsService.DeleteBrandByIds(ctx,brandIds)
	if err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateBrand 更新品牌管理
// @Tags Brand
// @Summary 更新品牌管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body brand.Brand true "更新品牌管理"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /brands/updateBrand [put]
func (brandsApi *BrandApi) UpdateBrand(c *gin.Context) {
    // 从ctx获取标准context进行业务行为
    ctx := c.Request.Context()

	var brands brand.Brand
	err := c.ShouldBindJSON(&brands)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = brandsService.UpdateBrand(ctx,brands)
	if err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindBrand 用id查询品牌管理
// @Tags Brand
// @Summary 用id查询品牌管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param brandId query int true "用id查询品牌管理"
// @Success 200 {object} response.Response{data=brand.Brand,msg=string} "查询成功"
// @Router /brands/findBrand [get]
func (brandsApi *BrandApi) FindBrand(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	brandId := c.Query("brandId")
	rebrands, err := brandsService.GetBrand(ctx,brandId)
	if err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:" + err.Error(), c)
		return
	}
	response.OkWithData(rebrands, c)
}
// GetBrandList 分页获取品牌管理列表
// @Tags Brand
// @Summary 分页获取品牌管理列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query brandReq.BrandSearch true "分页获取品牌管理列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /brands/getBrandList [get]
func (brandsApi *BrandApi) GetBrandList(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

	var pageInfo brandReq.BrandSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := brandsService.GetBrandInfoList(ctx,pageInfo)
	if err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败:" + err.Error(), c)
        return
    }
    response.OkWithDetailed(response.PageResult{
        List:     list,
        Total:    total,
        Page:     pageInfo.Page,
        PageSize: pageInfo.PageSize,
    }, "获取成功", c)
}

// GetBrandPublic 不需要鉴权的品牌管理接口
// @Tags Brand
// @Summary 不需要鉴权的品牌管理接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /brands/getBrandPublic [get]
func (brandsApi *BrandApi) GetBrandPublic(c *gin.Context) {
    // 创建业务用Context
    ctx := c.Request.Context()

    // 此接口不需要鉴权
    // 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
    brandsService.GetBrandPublic(ctx)
    response.OkWithDetailed(gin.H{
       "info": "不需要鉴权的品牌管理接口信息",
    }, "获取成功", c)
}
